package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.MenuReqDto;
import com.itheima.entity.*;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.PermissionMenuExtendMapper;
import com.itheima.mapper.PermissionPointExtendMapper;
import com.itheima.service.PermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private PermissionMenuExtendMapper permissionMenuExtendMapper;
    @Autowired
    private PermissionPointExtendMapper permissionPointExtendMapper;

    @Override
    public void modify(MenuReqDto menuReqDto) {
        Long id = menuReqDto.getId();
        Permission permission = permissionMapper.selectById(id);
        permission.setName(menuReqDto.getTitle());
        permission.setPid(menuReqDto.getPid());
        Boolean is_point = menuReqDto.getIs_point();
        if (is_point) {
            Long permissionPointExtendId = permission.getPermissionPointExtendId();
            PermissionPointExtend permissionPointExtend = permissionPointExtendMapper.selectById(permissionPointExtendId);
            permissionPointExtend.setCode(menuReqDto.getCode());
            permissionPointExtendMapper.updateById(permissionPointExtend);
        } else {
            Long permissionMenuExtendId = permission.getPermissionMenuExtendId();
            PermissionMenuExtend permissionMenuExtend = permissionMenuExtendMapper.selectById(permissionMenuExtendId);
            permissionMenuExtend.setCode(menuReqDto.getCode());
            permissionMenuExtendMapper.updateById(permissionMenuExtend);
        }
    }

    @Override
    public List<Zuigao> findAll() {
        List<Zuigao> zuigaos = new ArrayList<>();
        LambdaQueryWrapper<Permission> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.isNull(Permission::getPid);
        List<Permission> permissions1 = permissionMapper.selectList(wrapper1);
        for (Permission permission1 : permissions1) {
            Zuigao zuigao = new Zuigao();
            zuigao.setId(permission1.getId());
            zuigao.setPid(permission1.getPid());
            zuigao.setTitle(permission1.getName());
            Long permissionMenuExtendId = permission1.getPermissionMenuExtendId();
            PermissionMenuExtend permissionMenuExtend = permissionMenuExtendMapper.selectById(permissionMenuExtendId);
            zuigao.setCode(permissionMenuExtend.getCode());
            List<Child> childs = new ArrayList<>();
            LambdaQueryWrapper<Permission> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Permission::getPid, permission1.getId());
            List<Permission> permissions2 = permissionMapper.selectList(wrapper2);
            for (Permission permission2 : permissions2) {
                Child child = new Child();
                child.setId(permission2.getId());
                child.setPid(permission2.getPid());
                child.setTitle(permission2.getName());
                Long permissionMenuExtendId1 = permission2.getPermissionMenuExtendId();
                PermissionMenuExtend permissionMenuExtend1 = permissionMenuExtendMapper.selectById(permissionMenuExtendId1);
                child.setCode(permissionMenuExtend1.getCode());
                List<Point> points = new ArrayList<>();
                LambdaQueryWrapper<Permission> wrapper3 = new LambdaQueryWrapper<>();
                wrapper3.eq(Permission::getPid, permission2.getId());
                List<Permission> permissions3 = permissionMapper.selectList(wrapper3);
                for (Permission permission3 : permissions3) {
                    Point point = new Point();
                    point.setId(permission3.getId());
                    point.setPid(permission3.getPid());
                    point.setTitle(permission3.getName());
                    Long permissionPointExtendId = permission3.getPermissionPointExtendId();
                    PermissionPointExtend permissionPointExtend = permissionPointExtendMapper.selectById(permissionPointExtendId);
                    point.setCode(permissionPointExtend.getCode());
                    points.add(point);
                }
                child.setPoints(points);
                childs.add(child);
            }
            zuigao.setChilds(childs);
            zuigaos.add(zuigao);
        }
        return zuigaos;
    }

    @Override
    public MenuReqDto findMenu(Long id) {
        Permission permission = permissionMapper.selectById(id);
        MenuReqDto menuReqDto = new MenuReqDto();
        menuReqDto.setId(id);
        menuReqDto.setTitle(permission.getName());
        menuReqDto.setPid(permission.getPid());
        if (permission.getPermissionMenuExtendId() != null) {
            menuReqDto.setIs_point(false);
            Long permissionMenuExtendId = permission.getPermissionMenuExtendId();
            PermissionMenuExtend permissionMenuExtend = permissionMenuExtendMapper.selectById(permissionMenuExtendId);
            menuReqDto.setCode(permissionMenuExtend.getCode());
        }
        if (permission.getPermissionPointExtendId() != null) {
            menuReqDto.setIs_point(true);
            Long permissionPointExtendId = permission.getPermissionPointExtendId();
            PermissionPointExtend permissionPointExtend = permissionPointExtendMapper.selectById(permissionPointExtendId);
            menuReqDto.setCode(permissionPointExtend.getCode());
        }
        return menuReqDto;
    }

    @Override
    public void deleteById(Long id) {
        Permission permission = permissionMapper.selectById(id);
        Long permissionMenuExtendId = permission.getPermissionMenuExtendId();
        Long permissionPointExtendId = permission.getPermissionPointExtendId();
        permissionMapper.closeForeignKey();
        if (permissionMenuExtendId != null) {

            permissionMenuExtendMapper.deleteById(permissionMenuExtendId);
        }
        if (permissionPointExtendId != null) {
            permissionPointExtendMapper.deleteById(permissionPointExtendId);
        }
        permissionMapper.deleteById(id);
        permissionMapper.openForeignKey();

    }

    @Override
    public MenuReqDto add(MenuReqDto menuReqDto) {
        Boolean is_point = menuReqDto.getIs_point();
        Permission permission = new Permission();
        if (menuReqDto.getPid() != 0) {
            permission.setPid(menuReqDto.getPid());
        }
        permission.setName(menuReqDto.getTitle());
        permission.setCreateTime(new Date());
        permissionMapper.insert(permission);
        if (is_point) {
            PermissionPointExtend permissionPointExtend = new PermissionPointExtend();
            permissionPointExtend.setCode(menuReqDto.getCode());
            permissionPointExtend.setPermissionId(permission.getId());
            permissionPointExtendMapper.insert(permissionPointExtend);
            permission.setPermissionPointExtendId(permissionPointExtend.getId());
            permission.setType(2);
        } else {
            PermissionMenuExtend permissionMenuExtend = new PermissionMenuExtend();
            permissionMenuExtend.setCode(menuReqDto.getCode());
            permissionMenuExtend.setPermissionId(permission.getId());
            permissionMenuExtendMapper.insert(permissionMenuExtend);
            permission.setPermissionMenuExtendId(permissionMenuExtend.getId());
            permission.setType(1);
        }
        permissionMapper.updateById(permission);
        MenuReqDto menuReqDto1 = new MenuReqDto();
        menuReqDto1.setId(permission.getId());
        return menuReqDto1;
    }
}
